package tmproject.hlhj.fhp.cartrans.utils

import android.content.Context
import android.graphics.Bitmap
import android.os.Environment
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*
import android.content.Intent
import android.provider.MediaStore
import android.R.attr.bitmap
import android.net.Uri
import android.os.Build
import tmproject.hlhj.fhp.cartrans.log


/**
 * Created by Never Fear   on 2018\9\12 0012.
Never More....
 */
object FileUtils {
    /*创建图片*/
    @Throws(IOException::class)
    fun createImageFile(b: Bitmap, context: Context): File {
        // Create an image file name
        val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(Date())
        val imageFileName = "JPEG_" + timeStamp + "_"
        //.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录，一般放一些长时间保存的数据
        val storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        //创建临时文件,文件前缀不能少于三个字符,后缀如果为空默认未".tmp"
        val image = File.createTempFile(
                imageFileName, /* 前缀 */
                ".jpg", /* 后缀 */
                storageDir      /* 文件夹 */
        )
        val mCurrentPhotoPath = image.absolutePath
        var fOut: FileOutputStream? = null
        try {
            fOut = FileOutputStream(image)
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        }

        b.compress(Bitmap.CompressFormat.PNG, 100, fOut)
        try {
            fOut!!.flush()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        try {
            fOut!!.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return image
    }

    fun saveBitmap(bitmap: Bitmap,bitName:String,c:Context){
        val fileName: String
        val file: File
        if (Build.BRAND == "Xiaomi") { // 小米手机
            fileName = Environment.getExternalStorageDirectory().path + "/DCIM/Camera/" + bitName
        } else {  // Meizu 、Oppo
            fileName = Environment.getExternalStorageDirectory().path + "/DCIM/" + bitName
        }
        file = File(fileName)

        if (file.exists()) {
            file.delete()
        }
        val out: FileOutputStream
        try {
            out = FileOutputStream(file)
            // 格式为 JPEG，照相机拍出的图片为JPEG格式的，PNG格式的不能显示在相册中
            if (bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)) {
                out.flush()
                out.close()
                // 插入图库
                MediaStore.Images.Media.insertImage(c.getContentResolver(), file.absolutePath, bitName, null)
                log("fhp","保存成功")
                ToastUtils.toast("保存成功")
            }
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()

        }

        // 发送广播，通知刷新图库的显示
        c.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://$fileName")))

    }


}